package com.silong.swing.myWindows;

import javafx.geometry.Orientation;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by 龙哥 on 2022-01-17 15:38
 * Silong Software.
 */

public class ProgressBarFrame extends JFrame {

    JButton button1;
    JButton button2;

    JProgressBar pgbMission; //确定模式下进度条
    JProgressBar pgbIndeterminate;//创建不确定进度条

    public ProgressBarFrame(){
        init();
    }

    private void init(){

        button1 = new JButton("点击改变");
        button2 = new JButton("退出");
        pgbMission = new JProgressBar();
        pgbIndeterminate = new JProgressBar();

        Container window = this.getContentPane();
        this.setSize(800, 600);
        this.setLocationRelativeTo(null); //距中
        this.setTitle("自定义控件大小位置");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //退出窗口关闭程序
        //设置布局为空，优点:摆放灵活，缺点：代码量大，自适应能力差。
        this.setLayout(null);

        //设置确定进度条大小
        pgbMission.setLocation(100,150);
        pgbMission.setSize(500,50);
        pgbMission.setFont(new Font("黑体", Font.BOLD, 20));
        //配置进度条
        pgbMission.setMaximum(10);
        pgbMission.setMinimum(0);
        pgbMission.setValue(0);
        pgbMission.setStringPainted(true); //是否显示文本或者显示进度百分比
        pgbMission.setString("任务完成"); //进度条上显示的文字，如果不设置显示百分比。
        pgbMission.setForeground(Color.BLUE);
        pgbMission.setBackground(Color.GRAY);
        pgbMission.setOrientation(0); //进度条的方向 0=水平，1=垂直

        //设置不确定进度条大小
        pgbIndeterminate.setLocation(100,210);
        pgbIndeterminate.setSize(500,50);
        pgbIndeterminate.setFont(new Font("黑体", Font.BOLD, 20));
        //设定为不确定进度条
        pgbIndeterminate.setIndeterminate(true);

        //自定义按钮的位置 ，大小
        button1.setLocation(100,50); //设置按钮位置
        button1.setSize(200,50); //设置按钮大小
        button1.setFont(new Font("黑体", Font.BOLD, 20));
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //点击后，进度条 +1
                if(pgbMission.getValue() < pgbMission.getMaximum()){
                    pgbMission.setValue(pgbMission.getValue() + 1);
                }
            }
        });

        button2.setFont(new Font("黑体", Font.BOLD, 20));
        button2.setBounds(400, 50, 200, 50); //一次性设置位置与大小
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        //添加进度条的改变监听
        pgbMission.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                System.out.println("当前进度条：" + pgbMission.getValue());
                //进度条值变化时输出数据
                if(pgbMission.getValue() == pgbMission.getMaximum()){
                    pgbMission.setString("任务完成！");
                }
            }
        });
        window.add(button1);
        window.add(button2);
        window.add(pgbMission);
        window.add(pgbIndeterminate);

    }

    public static void main(String[] args) {
        ProgressBarFrame myFrame = new ProgressBarFrame();
        myFrame.setVisible(true);
    }
}
